﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;


namespace Giaobanapp
{
    public partial class phiengiaoban : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            load_data();
            
            if (!IsPostBack)
            {
                load_date();
                load_listtn();
                load_listdonvi();
                load_listtontai();
            }
        }
        protected void load_date()
        {
            txtdate.Text = DateTime.Now.ToString("d/M/yyyy");
        }

        protected void load_data()
        {
            string consql;
            consql = "select * from tblgiaoban";
            string s = WebConfigurationManager.ConnectionStrings["md"].ToString();
            SqlConnection con = new SqlConnection(s);
            con.Open();
            DataTable table = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(consql, con);
            adapter.Fill(table);
            gdata.DataSource = table;
            gdata.DataBind();
            con.Close();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string s = WebConfigurationManager.ConnectionStrings["md"].ToString();
            SqlConnection con = new SqlConnection(s);
            string sql = "insert into tblgiaoban(ngaygb,tentrongai,loaitrongai,diadiem,nguyennhan,giophatsinh,giokhoiphuc,bienphapxuly,tontai,donvi)";
            sql = sql + "values('" + txtdate.Text + "',N'" + txtnoidung.Text + "',N'" + ddlLoaiTroNgai.SelectedValue + "',N'" + txtdiadiem.Text + "',N'" + txtnguyennhan.Text + "','" + txtgiops.Text + "','" + txtgiokp.Text + "',N'" + txtbienphap.Text + "',N'" + ddltontai.Text + "',N'" + ddldonvi.Text + "')";
            SqlCommand cmd = new SqlCommand(sql, con);
            con.Open(); // mo ket noi
            cmd.ExecuteNonQuery(); // thuc thi
            con.Close();
            load_data();
            lblthongbao.Text = "Dữ liệu đã được cập nhật !";
            del_text();
        }
        protected void del_text()
        {
            txtnoidung.Text = "";
            txtnguyennhan.Text = "";
            txtdiadiem.Text = "";
            txtgiokp.Text = "";
            txtgiops.Text = "";
            txtdiadiem.Text = "";
            txtbienphap.Text = "";
            ddldonvi.Text = "";
            ddltontai.Text = "";
            ddlLoaiTroNgai.Text = "";
        }
        protected void load_listtn()//Hien thi danh sach loai tro ngai khi load form
        {
            string s1 = WebConfigurationManager.ConnectionStrings["md"].ToString();
            SqlConnection con = new SqlConnection(s1);
            con.Open();
            DataTable table1 = new DataTable();
            var dataadapter = new SqlDataAdapter("select loaitn from tblloaitn", con);
            var dataset = new DataSet();
            dataadapter.Fill(dataset);
            ddlLoaiTroNgai.DataSource = dataset;
            ddlLoaiTroNgai.DataTextField = "loaitn";
            ddlLoaiTroNgai.DataBind();
            con.Close();
        }

        protected void load_listtontai()//Hien thi danh sach loai ton tai khi load form
        {
            string s1 = WebConfigurationManager.ConnectionStrings["md"].ToString();
            SqlConnection con = new SqlConnection(s1);
            con.Open();
            DataTable table1 = new DataTable();
            var dataadapter = new SqlDataAdapter("select loaitontai from tbltontai", con);
            var dataset = new DataSet();
            dataadapter.Fill(dataset);
            this.ddltontai.DataSource = dataset;
            this.ddltontai.DataTextField = "loaitontai";
            this.ddltontai.DataBind();
            con.Close();
        }

        protected void load_listdonvi()//Hien thi danh sach loai ton tai khi load form
        {
            string s1 = WebConfigurationManager.ConnectionStrings["md"].ToString();
            SqlConnection con = new SqlConnection(s1);
            con.Open();
            DataTable table1 = new DataTable();
            var dataadapter = new SqlDataAdapter("select tendv from tbldonvi", con);
            var dataset = new DataSet();
            dataadapter.Fill(dataset);
            this.ddldonvi.DataSource = dataset;
            this.ddldonvi.DataTextField = "tendv";
            this.ddldonvi.DataBind();
            con.Close();
        }
        protected void gdata_RowDeleting1(object sender, GridViewDeleteEventArgs e)
        {
            string s = WebConfigurationManager.ConnectionStrings["md"].ToString();
            SqlConnection con = new SqlConnection(s);
            con.Open();
            int id = int.Parse(gdata.DataKeys[e.RowIndex].Value.ToString());
            SqlCommand cmd = new SqlCommand("delete from tblgiaoban where id='" + id + "'", con);
            cmd.ExecuteNonQuery();
            con.Close();
            lblthongbao.Text = " Xóa dữ liệu thành công!";
            load_data();
                           
        }

        protected void gdata_RowEditing1(object sender, GridViewEditEventArgs e)
        {
            gdata.EditIndex = Convert.ToInt16(e.NewEditIndex);
            load_data();
        }

        protected void gdata_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gdata.EditIndex = -1;
            load_data();
        }

        protected void gdata_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Chọn")
            {
                int num = Convert.ToInt16(e.CommandArgument);
                txtbienphap.Text = gdata.Rows[num].Cells[1].Text;
            }

        }

        protected void gdata_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if ((e.Row.Cells[8].Text.Contains("Đang")))
                {
                    e.Row.Cells[0].ForeColor = System.Drawing.Color.DarkOrange;
                    e.Row.Cells[1].ForeColor = System.Drawing.Color.DarkOrange;
                    e.Row.Cells[2].ForeColor = System.Drawing.Color.DarkOrange;
                    e.Row.Cells[3].ForeColor = System.Drawing.Color.DarkOrange;
                    e.Row.Cells[4].ForeColor = System.Drawing.Color.DarkOrange;
                    e.Row.Cells[5].ForeColor = System.Drawing.Color.DarkOrange;
                    e.Row.Cells[6].ForeColor = System.Drawing.Color.DarkOrange;
                    e.Row.Cells[7].ForeColor = System.Drawing.Color.DarkOrange;
                    e.Row.Cells[8].ForeColor = System.Drawing.Color.DarkOrange;
                    e.Row.Cells[9].ForeColor = System.Drawing.Color.DarkOrange;
                    e.Row.Cells[10].ForeColor = System.Drawing.Color.DarkOrange;
                }

                else if ((e.Row.Cells[8].Text.Contains("Chờ")))
                {
                    e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;
                    e.Row.Cells[1].ForeColor = System.Drawing.Color.Red;
                    e.Row.Cells[2].ForeColor = System.Drawing.Color.Red;
                    e.Row.Cells[3].ForeColor = System.Drawing.Color.Red;
                    e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
                    e.Row.Cells[5].ForeColor = System.Drawing.Color.Red;
                    e.Row.Cells[6].ForeColor = System.Drawing.Color.Red;
                    e.Row.Cells[7].ForeColor = System.Drawing.Color.Red;
                    e.Row.Cells[8].ForeColor = System.Drawing.Color.Red;
                    e.Row.Cells[9].ForeColor = System.Drawing.Color.Red;
                    e.Row.Cells[10].ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    e.Row.Cells[8].ForeColor = System.Drawing.Color.MidnightBlue;
                }
            }
        }

        protected void gdata_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (gdata.SelectedIndex > -1)
            {
                txtdate.Text = gdata.SelectedRow.Cells[1].Text;
                txtnoidung.Text = HttpUtility.HtmlDecode((string)(gdata.Rows[gdata.SelectedIndex].Cells[2].Text.ToString()));
                txtdiadiem.Text = HttpUtility.HtmlDecode((string)(gdata.Rows[gdata.SelectedIndex].Cells[4].Text.ToString()));
                txtgiops.Text = gdata.SelectedRow.Cells[5].Text;
                txtgiokp.Text = gdata.SelectedRow.Cells[6].Text;
                txtnguyennhan.Text = HttpUtility.HtmlDecode((string)(gdata.Rows[gdata.SelectedIndex].Cells[7].Text.ToString()));
                txtbienphap.Text = HttpUtility.HtmlDecode((string)(gdata.Rows[gdata.SelectedIndex].Cells[8].Text.ToString()));
            }
        }
                       
    }
}